package com.viewsky.base.back.util;

import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class Converter
{
	private static ObjectMapper om = new ObjectMapper();
	
	public static String toJson(Object object) throws Exception
	{
		String strJson = null;
		
		try
		{
			om.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
				
			strJson = om.writeValueAsString(object);
		}
		catch(Exception e)
		{
			throw new Exception(object + "，Json化字符串出错：" + e.toString());
		}
		
		return strJson;
	}
	
	@SuppressWarnings("unchecked")
	public static Map<String, Object> fromJson(String strOriginal) throws Exception
	{
		Map<String, Object> mapObject = new HashMap<>();
		
		try
		{
			mapObject = om.readValue(strOriginal, Map.class);
		}
		catch(Exception e)
		{
			throw new Exception(strOriginal + "，反Json化为map出错：" + e.toString());
		}
		
		return mapObject;
	}
	
	public String dealString(String strOriginal) 
	{
		return strOriginal;
	}
}
